package main

import "fmt"

func main() {
	var slice1 []int = make([]int, 5)
	for i := 0; i < len(slice1); i++ {
		slice1[i] = i
	}
	fmt.Println(slice1)
	for i, v := range slice1 {
		fmt.Println(i, v)
	}
	seasons := []string{"Spring", "Summer", "Fall", "Winter"}
	for i, v := range seasons {
		fmt.Println(i, v)
	}
	var season string
	for _, season = range seasons {
		fmt.Println(season)
	}

	array := [2][3]int{{1, 2, 3}, {4, 5, 6}}
	slice2 := array[:]
	for i, v := range slice2 {
		fmt.Println(i, v)
		for i, j := range v {
			fmt.Println(i, j)
		}
	}
	//切片重组
	slice3 := make([]int, 0, 10)
	for i := 0; i < cap(slice3); i++ {
		slice3 = slice3[0 : i+1]
		slice3[i] = i
		fmt.Printf("The length of slice is %d\n", len(slice3))
	}
	for i := 0; i < len(slice3); i++ {
		fmt.Println(slice3[i])
	}
	//切片复制
	slice4 := []int{1, 2, 3}
	slice5 := make([]int, len(slice4))
	copy(slice5, slice4)
	fmt.Println(slice5)

	//切片追加
	slice6 := []int{1, 2, 3}
	slice6 = append(slice6, 4)
	fmt.Println(slice6)
	//从字符串切片
	s := "hello world"
	substr := s[0:5]
	fmt.Println(substr)
	//使用切片修改字符串
	bs := []byte(s) //将字符串转换为字节数组 可修改
	bs[0] = 'H'
	substr = string(bs)
	fmt.Println(substr)
}
